/**
 * 
 */
package com.hs.hbp.action;

/**
 * @author yangbo
 *
 */
public class PageInfo {
	/**
	 * 总页数
	 */
	private int totalPage = 1;

	/**
	 * 前一页
	 */
	private int prePage = 1;

	/**
	 * 下一页
	 */
	private int nextPage = 1;

	/**
	 * 总记录数
	 */
	private int totalRec = 0;
	
	/**
	 * 默认每页记录数
	 */
	private final int defaultPageSize = 15;

	/**
	 * 每页记录数
	 */
	private int pageSize = defaultPageSize;

	/**
	 * 起始记录数
	 */
	private int startRec = 1;

	/**
	 * 结束记录数
	 */
	private int endRec = defaultPageSize;
	
	/**
	 * 当前页码
	 */
	private int pageIndex = 1;

	/**
	 * 全部页码，从1开始
	 */
	private int[] pageNumbers;

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex > 0 ? pageIndex : 1;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage > this.totalPage ? this.totalPage : nextPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize > 0 ? pageSize : 10;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage < 1 ? 1 : prePage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage > 0 ? totalPage : 1;
	}

	public int getTotalRec() {
		return totalRec;
	}

	public void setTotalRec(int totalRec) {
		this.totalRec = totalRec > -1 ? totalRec : 0;
	}

	public int[] getPageNumbers() {
		return pageNumbers;
	}

	public void setPageNumbers(int[] pageNumbers) {
		this.pageNumbers = pageNumbers;
	}

	public int getStartRec() {
		return (pageIndex - 1) * pageSize;
	}

	public int getEndRec() {
		return pageIndex * pageSize;
	}

	public static void main(String[] arg){
		// 记录总数
		int totalCount = 132;
		
		// 假定起始页
		int pageIndex = 4;
		
		System.out.println("假定起始页:"+pageIndex);
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageIndex(pageIndex);
		// 分页
		int s = pageInfo.getStartRec();//(pageInfo.getPageIndex() - 1) * pageInfo.getPageSize();
		int e = pageInfo.getEndRec();//pageInfo.getPageIndex() * pageInfo.getPageSize();
		
		e = e > totalCount ? totalCount : e;
		System.out.println("起始：" + s + ",结束：" + e);
		pageInfo.setTotalRec(totalCount);// 总记录数
		pageInfo
				.setTotalPage((pageInfo.getTotalRec()
						% pageInfo.getPageSize() == 0) ? (pageInfo
						.getTotalRec() / pageInfo.getPageSize())
						: (pageInfo.getTotalRec() / pageInfo.getPageSize()) + 1);// 总页数
		
		int totalPage = pageInfo.getTotalPage();
		System.out.println("总页数："+totalPage);
		
		// 假定最多显示10页
		int maxPage = 10;
		maxPage = totalPage > maxPage ? maxPage : totalPage;
		System.out.println("假定最多显示页:"+maxPage);
		
		// 额定递推数字
		int pushNum = 5;
		
		// 页数数组
		int[] pageNumbers = new int[maxPage];
		
		int startPage = pageIndex - pushNum > 0 ? pageIndex-pushNum:1;
		int endPage = pageIndex +5 > totalPage ? totalPage:pageIndex +5;
		System.out.println("分页起始页："+startPage);
		System.out.println("分页结束页："+endPage);
		
		for (int i = 1; i <= endPage; i++) {
			pageNumbers[i - 1] = startPage + i -1;
			System.out.println("分页页码：" +( startPage + i -1));
		}
		
		pageInfo.setPageNumbers(pageNumbers);
		pageInfo.setPrePage(pageInfo.getPageIndex() - 1);
		pageInfo.setNextPage(pageInfo.getPageIndex() + 1);
		
		System.out.println("上一页："+pageInfo.getPrePage());
		System.out.println("下一页："+pageInfo.getNextPage());
		
	}
}
